# -*- coding:utf-8 -*-
import ctypes
import threading
import MySQLdb
import os
import socket
import json
import httplib
import urllib2
import urllib
import uuid
import zipfile
import base64
import sys
import hashlib
import datetime
import time
import subprocess
import xml.dom.minidom
from biplist import *


def commom_log(filename, content , write_type='wb'):
    try:
        f = open(filename , write_type)
        f.write(content);
        f.close()
    except:
        print('happen an exception when write file')
        return ""

def getInfoPlist(random_filename):
    try:
        zf       = zipfile.ZipFile(random_filename, "r")
        nList    = zf.namelist()
        type_val = 0
        BundleId = ''
        CFBundleShortVersionString = ''
        CFBundleVersion = ''
        for f in nList:
            if f.rfind('Info.plist') != -1:
                f_arr = f.split("/")
                if len(f_arr) == 3:
                    if(f_arr[2] == 'Info.plist'):
                        info     = zf.read(f)
                        plist    = readPlistFromString(info)
                        BundleId = plist['CFBundleIdentifier']

                        if plist.has_key('CFBundleShortVersionString'):
                            CFBundleShortVersionString = plist['CFBundleShortVersionString']
                        else:
                            CFBundleShortVersionString = ''
                        if plist.has_key('CFBundleVersion'):
                            CFBundleVersion = plist['CFBundleVersion']
                        else:
                            CFBundleVersion = ''

                        print CFBundleVersion

        return BundleId,CFBundleShortVersionString,CFBundleVersion
    except:
        return '','',''


InfoPlist = getInfoPlist('d:/download_new.php')
if InfoPlist:
    print "==="
sys.exit()

db_host   = "192.168.11.6"
db_user   = 'xyzs'
db_passwd = 'ikF5NNeJg2G8'
db_port   = 3309
db_name   = "app_xyapplist"

conn      = MySQLdb.connect(host=db_host,user=db_user,passwd=db_passwd,port=db_port,init_command="set names utf8")
conn.select_db(db_name)
cur       = conn.cursor()
# status 2 ： 新增 3 ： 更新
sql       = "select itunesid from t_xyappsum where is_hot = 1;";
cur.execute(sql)
result = cur.fetchall()

for f in result:

    itunesid = f[0]

    m = hashlib.md5(str(itunesid)) 
    m.digest()
    md5_str = m.hexdigest()
    xxxx = md5_str[0: 1]
    str1 = md5_str[0: 2]
    str2 = md5_str[2: 4]

    path_zhengban = '/data/app/ipas/'+str1+"/"+str2+"/"+str(md5_str) + str2 + str1 + '.ipa'
    path_yueyu    = '/data/app/jailbreak/'+str1+"/"+str2+"/"+str(itunesid)+ '.ipa'
    path_sign     = '/data/app/ipas/'+str1+"/"+str2+"/"+str(md5_str) + str2 + str1 + '_s.ipa'

    if os.path.exists(path_zhengban):
        InfoPlist = getInfoPlist(path_zhengban)
        if InfoPlist[0]:
            sql_sum = "update t_xyappsum set bundleid = '"+InfoPlist[0]+"',version='"+InfoPlist[1]+"',bundleVersion='"+InfoPlist[2]+"' where itunesid = '"+str(itunesid)+"';";
            sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set bundleid = '"+InfoPlist[0]+"',version='"+InfoPlist[1]+"',bundleVersion='"+InfoPlist[2]+"' where itunesid = '"+str(itunesid)+"';";
            cur.execute(sql_app)
            cur.execute(sql_sum)
        else:
            sql_sum = "update t_xyappsum set is_install = 0 where itunesid = '"+str(itunesid)+"';";
            sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set is_install = 0 where itunesid = '"+str(itunesid)+"';";
            cur.execute(sql_app)
            cur.execute(sql_sum)
    else:
        sql_sum = "update t_xyappsum set is_install = 0 where itunesid = '"+str(itunesid)+"';";
        sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set is_install = 0 where itunesid = '"+str(itunesid)+"';";
        cur.execute(sql_app)
        cur.execute(sql_sum)

    print sql_sum + "\n"
    print sql_app + "\n"

    sql_sum = ''
    sql_app = ''

    if os.path.exists(path_yueyu):
        InfoPlist = getInfoPlist(path_yueyu)
        if InfoPlist[0]:
            sql_sum = "update t_xyappsum set version_j='"+InfoPlist[1]+"',bundleVersion_j='"+InfoPlist[2]+"' where itunesid = '"+str(itunesid)+"';";
            sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set version_j='"+InfoPlist[1]+"',bundleVersion_j='"+InfoPlist[2]+"' where itunesid = '"+str(itunesid)+"';";
            cur.execute(sql_app)
            cur.execute(sql_sum)
        else:
            sql_sum = "update t_xyappsum set is_j_install = 0 where itunesid = '"+str(itunesid)+"';";
            sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set is_j_install = 0 where itunesid = '"+str(itunesid)+"';";
            cur.execute(sql_app)
            cur.execute(sql_sum)
    else:
        sql_sum = "update t_xyappsum set is_j_install = 0 where itunesid = '"+str(itunesid)+"';";
        sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set is_j_install = 0 where itunesid = '"+str(itunesid)+"';";
        cur.execute(sql_app)
        cur.execute(sql_sum)


    print sql_sum + "\n"
    print sql_app + "\n"

    sql_sum = ''
    sql_app = ''

    if os.path.exists(path_sign):
        InfoPlist = getInfoPlist(path_sign)
        if InfoPlist[0]:
            sql_sum = "update t_xyappsum set  version_s='"+InfoPlist[1]+"',bundleVersion_s='"+InfoPlist[2]+"' where itunesid = '"+str(itunesid)+"';";
            sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set version_s='"+InfoPlist[1]+"',bundleVersion_s='"+InfoPlist[2]+"' where itunesid = '"+str(itunesid)+"';";
            cur.execute(sql_app)
            cur.execute(sql_sum)
        else:
            sql_sum = "update t_xyappsum set is_s_install = 0 where itunesid = '"+str(itunesid)+"';";
            sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set is_s_install = 0 where itunesid = '"+str(itunesid)+"';";
            cur.execute(sql_app)
            cur.execute(sql_sum)
    else:
        sql_sum = "update t_xyappsum set is_s_install = 0 where itunesid = '"+str(itunesid)+"';";
        sql_app = "update app_xyapplist_"+str(xxxx) + ".t_xyapplist_"+str(str1)+" set is_s_install = 0 where itunesid = '"+str(itunesid)+"';";
        cur.execute(sql_app)
        cur.execute(sql_sum)

    print sql_sum + "\n"
    print sql_app + "\n"

    sql_sum = ''
    sql_app = ''

conn.commit()
#关闭连接，释放资源   
cur.close()
conn.close()


